package com.dalei.vds.io;

import java.io.File;
import android.os.Environment;

public class Dirctory {

	public static File rootDirctory() {
		File sdcard = new File(Environment.getExternalStorageDirectory()
				.getPath());
		if (sdcard.exists() && sdcard.canWrite()) {
			File vds = new File(sdcard, "VDS");
			if (vds.exists()) {
				return vds;
			} else {
				if (vds.mkdir()) {
					return vds;
				} else {
					return null;
				}
			}
		} else {
			return null;
		}
	}
	
	public static File cacheDirctory() {
		File root = rootDirctory();
		if (root != null) {
			File cache = new File(root, "cache");
			if (cache.exists()) {
				return cache;
			} else {
				if (cache.mkdir()) {
					return cache;
				} else {
					return null;
				}
			}
		} else {
			return null;
		}
	}
	
	public static boolean clearCacheDirctory() {
		File cache = cacheDirctory();
		if (cache != null) {
			File[] files = cache.listFiles();
			for (File file : files) {
				if (file.delete() == false) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
	
	public static File imagesDirctory() {
		File root = rootDirctory();
		if (root != null) {
			File temp = new File(root, "images");
			if (temp.exists()) {
				return temp;
			} else {
				if (temp.mkdir()) {
					return temp;
				} else {
					return null;
				}
			}
		} else {
			return null;
		}
	}
	
	public static boolean clearImagesDirctory() {
		File images = imagesDirctory();
		if (images != null) {
			File[] files = images.listFiles();
			for (File file : files) {
				if (file.delete() == false) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
	
	public static File tempDirctory() {
		File root = rootDirctory();
		if (root != null) {
			File temp = new File(root, "temp");
			if (temp.exists()) {
				return temp;
			} else {
				if (temp.mkdir()) {
					return temp;
				} else {
					return null;
				}
			}
		} else {
			return null;
		}
	}
	
	public static boolean clearTempDirctory() {
		File temp = tempDirctory();
		if (temp != null) {
			File[] files = temp.listFiles();
			for (File file : files) {
				if (file.delete() == false) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
}
